/**
 * @author raelzhu
 * @version $Revision: 1.22 $
 */
$import('capability/capability.js'); // for older browsers, eg. IE 5.0

/** 3rd-party libraries **/
$import('yui/yahoo-dom-event/yahoo-dom-event.js');
$import('prototype/prototype-1.4.0.js'); // Based-on Prototype JavaScript framework
$import('scriptaculous/builder.js');
$import('scriptaculous/effects.js');

$import('namespaces/namespaces.js');
$import('logging/logger.js');
$import('validation/validator.js');
$import('util/util.js');
$import('ajax/ajax.js');
$import('core/core.js');
$import('core/dcms3.js');
//$import('core/dataBinding.js');
//$import('sandbox/sandbox.js');

/** DCMS3 Legacy & Integration Library **/
$import('migration/migration.js');
$import('migration/constant.jsp');
$import('../../css/cn/script.js');

/*** Calendar Library ***/
$import('calendar/calendar.js');
$import('calendar/calendar-util.js');

/*** MessageBox Library ***/
//$import('lightbox/lightbox.js');
$import('lightbox/messagebox.js');

/*** YUI Library ***/
$import('yui/treeview/treeview.js');
$import('yui/treeview/TaskNode.js');
$import('yui/container/container.js');

if (!window.context) { var context = {}; }
if (!window.binding) { var binding = {}; }

function $import(path) {
	var basePath = '';
	var srcPath = 'opal.js';
	var scripts = document.getElementsByTagName('script');
	for (var i = 0; i < scripts.length; i++) {
		if (scripts[i].src.match(srcPath)) {
			basePath = scripts[i].src.replace(srcPath, '');
			break;
		}
	}
	document.writeln('<script src="' + basePath + path + '"></script>');
}

function $init_context(context) {
	// TODO: class hinting
	if (context && typeof context == 'object') {
		if (typeof context.map == 'object' && typeof context.map.length != 'number') {
			context = context.map;
		} else if (typeof context.list == 'object' && typeof context.list.length == 'number') {
			context = context.list;
		}
		for (var property in context) {
			if (property != 'javaClass') {
				context[property] = $init_context(context[property]);
			}
		}
	}
	return context;
}

function $populate_context(expression, value) {
	// preserve the data type
	var valueExpression = 'value';
	switch (typeof value) {
		case 'string':
			valueExpression = '"' + value + '"';
			break;
		case 'number':
			valueExpression = value;
			break;
	}
	Try.these(
		function() { eval('context.' + expression + '=' + valueExpression); },
		function() {
			if (expression.indexOf('.') > -1) {
				var beanExpression = expression.substring(0, expression.lastIndexOf('.'));
				eval('context.' + beanExpression + '= {}');
				eval('context.' + expression + '=' + valueExpression);
			}
		}
	);
}

function $sync_context() {
	// full-scope data synchronization
	for (id in binding) {
		var obj = eval(id);
		if (obj && typeof obj.syncData == 'function') {
			obj.syncData();
		}
	}
}

/*****************************************************************/
function $O(o) {
	if(typeof o == 'string') {
		try {
			return $O.componentMap[o] || eval(o) || $(o);
		} catch(e) {
			return null;
		}
	} else if(typeof o.index != 'undefined' && typeof o.id != 'undefined') {
		try {
			return $O.componentMap[o.id + o.index] || eval(o.id + o.index) || $(o.id + o.index);
		} catch(e) {
			return null;
		}
	} else {
		try {
			return $O.componentMap[o.id] || eval(o.id) || $(o.id);
		} catch(e) {
			return null;
		}
	}
}
$O.componentMap = {};
$O.registerComponent = function(id, component) {
	if ($O.componentMap[id]) {
		Opal.Log.warn(id + ' has existed and will be overrided!');
	}
	$O.componentMap[id] = component;
}
